/*
 * @Date: 2021-08-17 17:45:11
 * @information:
 */
import {
  request,
  summary,
  description, // 接口名称下方的描述信息
  query, // get时参数
  path, // post, put, delete 时地址栏参数
  body, // body中的参数
  tags
} from 'koa-swagger-decorator'
import LikeDiaryService from '../service/LikeDiaryService'


const likeDiaryService = new LikeDiaryService()
const tag = tags(['LikeDiary'])

export default class LikeDiaryController {
  @request('post', '/likeDiary/findByDiaryId')
  @summary('根据被点赞的数据去查询数据列表')
  @tag
  @body({
    diaryId: { type: 'string', required: true },
  })
  async findByDiaryId (ctx) {
    const bObj = ctx.request.body
    const data = await likeDiaryService.findByDiaryId(bObj)
    ctx.rest(data)
  }

  @request('post', '/likeDiary/isOrNotlikeDiary')
  @summary('添加点赞或取消点赞')
  @tag
  @body({
    diaryId: { type: 'string', required: true },
    toUserId: { type: 'string', required: true },
    fromUserId: { type: 'string', required: true },
    isLike: { type: 'boolean', required: true },
  })
  async likeDiary (ctx) {
    const bObj = ctx.request.body
    if (bObj.isLike === undefined || bObj.isLike === null) {
      throw { message: '参数缺失: isLike', code: 400 }
    }
    if (bObj.isLike === true) { // 添加点赞
      const res = await likeDiaryService.likeDiary(bObj)
      ctx.rest(res.id)
    } else { // 取消点赞
      const res = await likeDiaryService.unLikeDiary(bObj)
      ctx.rest()
    }
  }


}

